package com.yjz.study.adapter.home

import android.content.Context
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.yjz.common.extend.loadUrl
import com.yjz.log.util.DisplayUtil
import com.yjz.study.R
import com.yjz.study.arouter.ARouterLogic
import com.yjz.study.http.entity.res.Subcategory
import com.yjz.ui.recycler.item.HiDataItem
import com.yjz.ui.recycler.item.HiViewHolder

class GridItem(val list: List<Subcategory>) :
    HiDataItem<List<Subcategory>, HiViewHolder>(list) {
    override fun onBindData(holder: HiViewHolder, position: Int) {
        val context = holder.itemView.context
        val gridView = holder.itemView as RecyclerView
        gridView.adapter = GridAdapter(context, list)
    }

    override fun getItemView(parent: ViewGroup): View? {
        val gridView = RecyclerView(parent.context)
        val params = RecyclerView.LayoutParams(
            RecyclerView.LayoutParams.MATCH_PARENT,
            RecyclerView.LayoutParams.WRAP_CONTENT
        )
        params.bottomMargin = DisplayUtil.dp2px(10f)
        gridView.layoutManager = GridLayoutManager(parent.context, 5)
        gridView.layoutParams = params
        gridView.setBackgroundColor(Color.WHITE)
        return gridView
    }

    inner class GridAdapter(val context: Context, val list: List<Subcategory>) :
        RecyclerView.Adapter<HMyViewHolder>() {
        private var inflater = LayoutInflater.from(context)

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HMyViewHolder {
            val view = inflater.inflate(R.layout.layout_home_op_grid_item, parent, false)
            return HMyViewHolder(view)
        }

        override fun getItemCount(): Int {
            return list.size
        }

        override fun onBindViewHolder(holder: HMyViewHolder, position: Int) {
            val subcategory = list[position]
            holder.itemImage.loadUrl(subcategory.subcategoryIcon)
            holder.itemTitle.text = subcategory.subcategoryName
            holder.itemView.setOnClickListener {
                // 会跳转到子分类列表上面去，，是一个单独的页面
                val bundle = HashMap<String, String>()
                bundle["categoryId"] = subcategory.categoryId
                bundle["subcategoryId"] = subcategory.subcategoryId
                bundle["categoryTitle"] = subcategory.subcategoryName
                ARouterLogic.navigation(ARouterLogic.Path.GOOD_LIST, bundle)
            }
        }
    }

    class HMyViewHolder(val view: View) : RecyclerView.ViewHolder(view) {
        var itemImage: ImageView = view.findViewById(R.id.item_image)
        var itemTitle: TextView = view.findViewById(R.id.item_title)
    }
}